import { SendCode, register, LoginInterface, reqUseInfo, reqlogout } from "@/API/index"

const actions = {

    //注册验证码函数
    async CodeList({ commit }, phone) {
        let codeons = await SendCode(phone)
        if (codeons.code == 200) {
            console.log(codeons.data);
        }
    },



    //注册函数
    async passport({ commit }, usee) {
        let regist = await register(usee)
        if (regist.code == 200) {
            return true
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    },


    //登录函数
    async Blogin({ commit }, user) {
        let setLogin = await LoginInterface(user)
        if (setLogin.code == 200) {
            //持久化管理
            localStorage.setItem("token", setLogin.data.token);
            commit("UUIDTOKEN", setLogin.data.token);
            return true;
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    },


    //根据token获取用户信息
    async UseInfo({ commit }) {
        let tokenUseInfo = await reqUseInfo();
        if (tokenUseInfo.code == 200) {
            commit("UseInfoList", tokenUseInfo.data)
            return true
        }
        else {
            return Promise.reject(new Error('faile'));
        }
    },


    //退出登录
    async logout({ commit }) {
        let out = await reqlogout();
        if (out.code == 200) {
            return true
        }
        else {
            return Promise.reject(new Error('faile'))
        }
    }
}



const mutations = {
    UUIDTOKEN(state, token) {
        // console.log(data);
        return state.token = token;
    },
    UseInfoList(state, data) {
        return state.UseInfodata = data

    }
}
const state = {
    token: localStorage.getItem("token"),
    UseInfodata: {}
}
export default {
    actions,
    mutations,
    state,
}